<template>
  <div class="c-user-add">
    <c-row-crumb :title="multLang('btn_new_a', 'title_fu')">
      <el-button @click="clear">{{ multLang('btn_clear_a') }}</el-button>
      <el-button type="primary" plain @click="submit(true)">
        {{ multLang('保存并返回') }}
      </el-button>
      <el-button type="primary" @click="submit()">
        {{ multLang('btn_save_a') }}
      </el-button>
    </c-row-crumb>
    <c-row-add :title="tplLang('title_asset_e', ['title_fu'])">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="140px"
        :validate-on-rule-change="false"
      >
        <c-row :col="4">
          <c-col>
            <el-form-item :label="multLang('title_dg', ':')" prop="user_name">
              <el-input
                v-model="form.user_name"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item :label="multLang('title_jq', ':')" prop="account">
              <el-input
                v-model="form.account"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item :label="multLang('title_aah_f', ':')" prop="password">
              <el-input
                v-model="form.password"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item :label="multLang('text_ba', ':')" prop="gender_tag">
              <c-select
                v-model="form.gender_tag"
                :options="genderTagList"
                :placeholder="tplLang('text_choose_a', ['text_ba'])"
              ></c-select>
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item
              :label="multLang('title_baaab_h', ':')"
              prop="user_code"
            >
              <el-input
                v-model="form.user_code"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>

          <c-col>
            <el-form-item :label="multLang('title_fx', ':')" prop="mobile">
              <el-input
                v-model="form.mobile"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>

          <c-col>
            <el-form-item :label="multLang('text_ay', ':')" prop="join_date">
              <c-date-picker
                v-model="form.join_date"
                :placeholder="tplLang('text_choose_a', ['title_date_t'])"
              >
              </c-date-picker>
            </el-form-item>
          </c-col>

          <c-col>
            <el-form-item :label="multLang('title_gw', ':')" prop="email">
              <el-input
                v-model="form.email"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>

          <c-col>
            <el-form-item :label="multLang('text_az', '(¥/h)')" prop="hour_fee">
              <el-input
                v-model="form.hour_fee"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>

          <c-col>
            <el-form-item
              :label="multLang('title_ba_m', 'title_pf', ':')"
              prop="positionIds"
            >
              <c-mult-select
                v-model="form.positionIds"
                :options="groupPosition.data"
                :placeholder="tplLang('text_choose_a', ['title_pf'])"
              >
              </c-mult-select>
              <!-- <c-cascader
                multiple
                clearable
                show-all-levels
                v-model="form.positionIds"
                :options="groupPosition.data"
                :placeholder="tplLang('text_choose_a', ['title_pf'])"
              ></c-cascader> -->
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item :label="multLang('title_mp', ':')" prop="is_use">
              <el-radio-group v-model="form.is_use">
                <el-radio
                  v-for="item in isEnableList"
                  :key="item.value"
                  :label="item.value"
                >
                  {{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item
              :label="multLang('title_aau_m', ':')"
              prop="is_charge"
            >
              <el-checkbox v-model="form.is_charge"></el-checkbox>
            </el-form-item>
          </c-col>
          <c-col :col="11">
            <el-form-item :label="multLang('title_note_a', ':')" prop="remark">
              <c-input-text v-model="form.remark"></c-input-text>
            </el-form-item>
          </c-col>
        </c-row>
      </el-form>
    </c-row-add>
  </div>
</template>
<script>
import { addUserData } from '@/http/api/v1/user'
import { getPositionOption } from '@/http/api/v1/org'
import { encrypt } from '@/util/crypto'
export default {
  name: 'VUserAdd',
  components: {},
  props: {},
  data() {
    return {
      groupPosition: {
        data: [],
        map: {}
      },
      form: {
        user_name: '',
        account: '',
        password: '',
        gender_tag: '1',
        user_code: '',
        mobile: '',
        join_date: '',
        email: '',
        hour_fee: '',
        positionIds: [],
        is_use: '1',
        is_charge: false,
        remark: ''
      }
    }
  },
  computed: {
    genderTagList() {
      return this.optionList('gender_tag')
    },
    isEnableList() {
      return this.optionList('is_enable')
    },
    rules() {
      return {
        user_name: {
          required: true,
          message: this.tplLang('msg_a', ['title_dg']),
          trigger: 'blur'
        },
        account: {
          required: true,
          message: this.tplLang('msg_a', ['title_jq']),
          trigger: 'blur'
        },
        password: {
          required: true,
          message: this.tplLang('msg_a', ['title_aah_f']),
          trigger: 'blur'
        },
        gender_tag: {
          required: true,
          message: this.tplLang('msg_a', ['text_ba']),
          trigger: 'blur'
        },
        mobile: {
          required: true,
          message: this.tplLang('msg_a', ['title_fx']),
          trigger: 'blur'
        },
        positionIds: {
          required: true,
          message: this.tplLang('text_choose_a', ['title_pf']),
          trigger: 'change'
        },
        is_use: {
          required: true,
          message: this.tplLang('text_choose_a', ['title_mp']),
          trigger: 'change'
        }
      }
    }
  },
  watch: {},
  beforeCreate() {},
  created() {
    this.setOption({ key: 'gender_tag' })
    this.setOption({ key: 'is_enable' })
  },
  beforeMount() {},
  mounted() {},
  beforeUpdate() {},
  updated() {},
  activated() {
    this.getPositionOption()
  },
  deactivated() {},
  beforeDestroy() {},
  destroyed() {},
  methods: {
    clearForm() {},
    getPositionOption() {
      getPositionOption().then(({ data, map }) => {
        this.groupPosition.data = data
        this.groupPosition.map = map
      })
    },
    clear() {
      this.$refs.form.resetFields()
    },
    submit(back) {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { password, ...data } = this.form
          data.password = encrypt(password)
          addUserData(data).then(
            ({ code }) => code === 200 && back && this.$router.back()
          )
        }
      })
    }
  }
}
</script>
<style lang="scss"></style>
